% TODO proof-reading
\subsection{Exceptions}

Let's rework our \IT{Month} example (\myref{Java_2D_array_month}) a bit:

\begin{lstlisting}[caption=IncorrectMonthException.java,style=customjava]
public class IncorrectMonthException extends Exception
{
	private int index;

	public IncorrectMonthException(int index)
	{
		this.index = index;
	} 
	public int getIndex()
	{
		return index;
	}
}
\end{lstlisting}

\begin{lstlisting}[caption=Month2.java,style=customjava]
class Month2
{
	public static String[] months = 
	{
		"January", 
		"February", 
		"March", 
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"October",
		"November",
		"December"
	};

	public static String get_month (int i) throws IncorrectMonthException
	{
		if (i<0 || i>11)
			throw new IncorrectMonthException(i);
		return months[i];
	};

	public static void main (String[] args)
	{
		try
		{
			System.out.println(get_month(100));
		}
		catch(IncorrectMonthException e)
		{
			System.out.println("incorrect month index: "+ e.getIndex());
			e.printStackTrace();
		}
	};
}
\end{lstlisting}

Essentially, \TT{IncorrectMonthException.class} has just an object constructor 
and one getter method.

The \TT{IncorrectMonthException} class is derived from \TT{Exception}, 
so the \TT{IncorrectMonthException} constructor
first calls the constructor of the \TT{Exception} class, 
then it puts incoming integer value into the sole \TT{IncorrectMonthException} class field:

\begin{lstlisting}
  public IncorrectMonthException(int);
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0       
         1: invokespecial #1        // Method java/lang/Exception."<init>":()V
         4: aload_0       
         5: iload_1       
         6: putfield      #2        // Field index:I
         9: return        
\end{lstlisting}

\TT{getIndex()} is just a getter.
A \IT{reference} to \TT{IncorrectMonthException} is passed in the zeroth \ac{LVA} slot
(\IT{this}), \TT{aload\_0} takes it, \TT{getfield} loads an integer value from the object, 
\TT{ireturn} returns it.

\begin{lstlisting}
  public int getIndex();
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0       
         1: getfield      #2        // Field index:I
         4: ireturn       
\end{lstlisting}

Now let's take a look at \TT{get\_month()} in \TT{Month2.class}:

\begin{lstlisting}[caption=Month2.class]
  public static java.lang.String get_month(int) throws IncorrectMonthException;
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=1, args_size=1
         0: iload_0       
         1: iflt          10
         4: iload_0       
         5: bipush        11
         7: if_icmple     19
        10: new           #2        // class IncorrectMonthException
        13: dup           
        14: iload_0       
        15: invokespecial #3        // Method IncorrectMonthException."<init>":(I)V
        18: athrow        
        19: getstatic     #4        // Field months:[Ljava/lang/String;
        22: iload_0       
        23: aaload        
        24: areturn       
\end{lstlisting}

\TT{iflt} at offset 1 is \IT{if less than}.

In case of invalid index, a new object is created using the \TT{new} instruction at offset 10.

The object's type is passed as an operand to the instruction (which is \TT{IncorrectMonthException}).

Then its constructor is called, and index is passed via \ac{TOS} (offset 15).

When the control flow is offset 18, the object is already constructed, 
so now the \TT{athrow} instruction takes a \IT{reference} 
to the newly constructed object and signals to \ac{JVM} to find the appropriate exception handler.

The \TT{athrow} instruction doesn't return the control flow here, 
so at offset 19 there is another \gls{basic block},
not related to exceptions business, where we can get from offset 7.

How do handlers work?

\main in \TT{Month2.class}:

\begin{lstlisting}[caption=Month2.class]
  public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=2, args_size=1
         0: getstatic     #5        // Field java/lang/System.out:Ljava/io/PrintStream;
         3: bipush        100
         5: invokestatic  #6        // Method get_month:(I)Ljava/lang/String;
         8: invokevirtual #7        // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        11: goto          47
        14: astore_1      
        15: getstatic     #5        // Field java/lang/System.out:Ljava/io/PrintStream;
        18: new           #8        // class java/lang/StringBuilder
        21: dup           
        22: invokespecial #9        // Method java/lang/StringBuilder."<init>":()V
        25: ldc           #10       // String incorrect month index: 
        27: invokevirtual #11       // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        30: aload_1       
        31: invokevirtual #12       // Method IncorrectMonthException.getIndex:()I
        34: invokevirtual #13       // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
        37: invokevirtual #14       // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        40: invokevirtual #7        // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        43: aload_1       
        44: invokevirtual #15       // Method IncorrectMonthException.printStackTrace:()V
        47: return        
      Exception table:
         from    to  target type
             0    11    14   Class IncorrectMonthException
\end{lstlisting}

Here is the \TT{Exception table}, which defines that from offsets 0 to 11 (inclusive) an exception \\
\TT{IncorrectMonthException} may happen, and if it does, the control flow is to be passed to offset 14.

Indeed, the main program ends at offset 11.

At offset 14 the handler starts. It's not possible to get here, 
there are no conditional/unconditional jumps to this area.

But \ac{JVM} will transfer the execution flow here in case of an exception.

The very first \TT{astore\_1} (at 14) takes the incoming \IT{reference} to the exception object 
and stores it in \ac{LVA} slot 1.

Later, the \TT{getIndex()} method (of this exception object) will be called at offset 31.

The \IT{reference} to the current exception object is passed right before that (offset 30).

The rest of the code is does just string manipulation: 
first the integer value returned by \TT{getIndex()}
is converted to string by the \TT{toString()} method, then it's concatenated with 
the \q{incorrect month index: } text string (like we saw before),
then \TT{println()} and \TT{printStackTrace()} are called.

After \TT{printStackTrace()} finishes, the exception is handled and we can continue with the normal execution.

At offset 47 there is a \TT{return} which finishes the \main function, 
but there could be any other code which would execute as if no exceptions were raised.

Here is an example on how IDA shows exception ranges:


\begin{lstlisting}[caption=from some random .class file found on the author's computer]
    .catch java/io/FileNotFoundException from met001_335 to met001_360\
 using met001_360
    .catch java/io/FileNotFoundException from met001_185 to met001_214\
 using met001_214
    .catch java/io/FileNotFoundException from met001_181 to met001_192\
 using met001_195
    .catch java/io/FileNotFoundException from met001_155 to met001_176\
 using met001_176
    .catch java/io/FileNotFoundException from met001_83 to met001_129 using \
met001_129
    .catch java/io/FileNotFoundException from met001_42 to met001_66 using \
met001_69
    .catch java/io/FileNotFoundException from met001_begin to met001_37\
 using met001_37
\end{lstlisting}
